package com.ruoyi.user.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.user.domain.NearbyMerchant;
import com.ruoyi.user.domain.MerchantDetail;
import com.ruoyi.user.service.INearbyMerchantService;
import com.ruoyi.user.service.IAmapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 小程序-附近商家接口
 */
@RestController
@RequestMapping("/user/merchants/nearby")
public class NearbyMerchantController {

    @Autowired
    private INearbyMerchantService nearbyMerchantService;

    @Autowired
    private IAmapService amapService;

    /**
     * 根据区域ID查询附近商家信息
     */
    @PreAuthorize("@ss.hasPermi('user:nearby:list')")
    @GetMapping("/by-region/{regionId}")
    public AjaxResult listByRegion(@PathVariable("regionId") Long regionId) {
        List<NearbyMerchant> list = nearbyMerchantService.listByRegionId(regionId);
        return AjaxResult.success(list);
    }

    /**
     * 根据商家ID查询商家详情（含菜品）
     */
    @PreAuthorize("@ss.hasPermi('user:nearby:detail')")
    @GetMapping("/detail/{merchantId}")
    public AjaxResult detail(@PathVariable("merchantId") Long merchantId) {
        MerchantDetail detail = nearbyMerchantService.getDetailByMerchantId(merchantId);
        return AjaxResult.success(detail);
    }





    /**
     * 根据区域ID与商家类型查询附近商家
     */
    @PreAuthorize("@ss.hasPermi('user:nearby:type')")
    @GetMapping("/by-region/{regionId}/by-type/{type}")
    public AjaxResult listByRegionAndType(@PathVariable("regionId") String regionId,
                                          @PathVariable("type") String type) {
        List<NearbyMerchant> list = nearbyMerchantService.listByRegionAndType(regionId, type);
        return AjaxResult.success(list);
    }

    /**
     * 根据当前用户获取regionId
     */
    @PreAuthorize("@ss.hasPermi('user:nearby:query')")
    @GetMapping("/get-regionId/by-current")
    public AjaxResult listByCurrentUser() {
        Long regionId = nearbyMerchantService.regionIdByCurrentUser();

        return AjaxResult.success(regionId);
    }

    /*
    * 获取商家到用户的距离
    * */
    @PreAuthorize("@ss.hasPermi('user:nearby:distance')")
    @GetMapping("/distance/driving")
    public AjaxResult drivingDistanceKm(@RequestParam("origin") String origin,
                                        @RequestParam("destination") String destination,
                                        @RequestParam(value = "strategy", required = false) String strategy) {
        java.util.Map<String, Object> data = amapService.drivingPolylineData(origin, destination, strategy);
        Object d = data.get("distance");
        if (d == null) {
            return AjaxResult.success(null);
        }
        java.math.BigDecimal meters = new java.math.BigDecimal(d.toString());
        java.math.BigDecimal km = meters.divide(new java.math.BigDecimal("1000"), 2, java.math.RoundingMode.HALF_UP);
        java.util.Map<String, Object> resp = new java.util.HashMap<>();
        resp.put("distanceKm", km);
        return AjaxResult.success(resp);
    }
}
